/*
   Copyright (c) 2023 李伟国
   shiro-learner is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details. 
*/

package cn.learner.controller;

import cn.learner.config.response.ErrorResponseData;
import cn.learner.config.response.ResponseData;
import cn.learner.config.response.SuccessResponseData;
import cn.learner.constant.enumeration.StatusConstantEnum;
import cn.learner.controller.base.BaseController;
import cn.learner.dto.UserRoleDto;
import cn.learner.entity.Role;
import cn.learner.service.IRoleService;
import cn.learner.service.IUserRoleService;
import cn.learner.utils.ObjectCast;
import com.alibaba.druid.support.json.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "role")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IUserRoleService userRoleService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResponseData roleList(){
        return new SuccessResponseData(roleService.findAll());
    }

    @RequestMapping(value = "options", method = RequestMethod.GET)
    public ResponseData roleListOptions(){
        return new SuccessResponseData(roleService.findRoleAllOptions());
    }

    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public ResponseData updateRole(@RequestBody Role role){
        return new SuccessResponseData(roleService.updateRole(role));
    }

    @RequestMapping(value = "info/{roleId}", method = RequestMethod.GET)
    public ResponseData getRole(@PathVariable("roleId") Long roleId){
        return new SuccessResponseData(roleService.getById(roleId));
    }

    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public ResponseData insertRole(@RequestBody Role role){
        if(role == null || role.getName() == null){
            return new ErrorResponseData();
        }
        role.setStatus(StatusConstantEnum.ENABLE.getCode());
        return new SuccessResponseData(roleService.save(role));
    }

    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    public ResponseData deleteRole(@RequestBody Role role){
        return new SuccessResponseData(this.roleService.deleteRole(role));
    }

    @RequestMapping(value = "reset", method = RequestMethod.POST)
    public ResponseData resetRole(@RequestBody Role role){
        return new SuccessResponseData(this.roleService.resetRole(role));
    }

    @RequestMapping(value = "bind/user", method = RequestMethod.POST)
    public ResponseData bindUserById(@RequestBody UserRoleDto userRoleDto){
        List<Long> longs = ObjectCast.castListToLong(JSONUtils.parse(userRoleDto.getRoleIds()));
        return new SuccessResponseData(userRoleService.bindByUserIdRoleIds(userRoleDto.getUserId(), longs));
    }

    @RequestMapping(value = "bind/user", method = RequestMethod.DELETE)
    public ResponseData bindUserByIdDelete(@RequestBody UserRoleDto userRoleDto){
        List<Long> longs = ObjectCast.castListToLong(JSONUtils.parse(userRoleDto.getRoleIds()));
        return new SuccessResponseData(userRoleService.deleteByUserIdRoleIds(userRoleDto.getUserId(), longs));
    }

    @RequestMapping(value = "bind/user/name/{userId}", method = RequestMethod.GET)
    public ResponseData getRoleNameListByUserId(@PathVariable("userId") Long userId){
        return new SuccessResponseData(roleService.findByUserId(userId));
    }

    @RequestMapping(value = "bind/user/{userId}", method = RequestMethod.GET)
    public ResponseData getRoleListByUserId(@PathVariable("userId") Long userId){
        return new SuccessResponseData(userRoleService.findUserRoleByUserId(userId));
    }
}
